package com.zxlspace.collection2.map;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * hashmap 经典存储-分拣思路实现
 * "分拣"思路
 * 1.为所有key创建容器
 *   之后容器中存放对应的value
 * 2.第一次创建容器，并存放value
 *   第二次之后，直接使用容器存放值
 */
public class MapLetter01 {


    public static void main(String[] args) {
        String str = "this is a cat and that is a mice and where is the food ?";
        String[] strArray = str.split(" ");
        //hashmap key 不重复，用来计数单词出现重复次数
        Map<String, Letter> letterMap = new HashMap<>();
//        test2();

        //第一次创建容器，并存放value
        // 第二次之后，直接使用容器存放值
       /* for (String temp : strArray) {
            if (!letterMap.containsKey(temp)){
                letterMap.put(temp, new Letter(temp, 1));
            }else {
                Letter letter = letterMap.get(temp);
                letter.setCount(letter.getCount() + 1);
            }
        }*/

        Letter col = null;
        for (String temp : strArray) {
            if ( null == (col = letterMap.get(temp)) ) {
                letterMap.put(temp, new Letter(temp, 1));  //第一次值存放容器中
            } else {
                col.setCount(col.getCount() + 1); //第二次之后，直接使用容器存放值
            }
        }

        Set<String> keyset = letterMap.keySet();
        for (String key : keyset) {
            Letter letter = letterMap.get(key);
            System.out.println( key +"出现的次数：" + letter.getCount());
        }
    }

    //思路1：为所有的容器创建key
    public static void test1() {
        String str = "this is a cat and that is a mice and where is the food ?";
        String[] strArray = str.split(" ");
        //hashmap key 不重复，用来计数单词出现重复次数
        Map<String, Letter> letterMap = new HashMap<>();

        for (String temp : strArray) {
            //为所有的key创建容器
            if (!letterMap.containsKey(temp)) {
                letterMap.put(temp, new Letter());
            }
        }

        for (String temp : strArray) {
            //之后存放对应的value，容器key重复计数
            if (letterMap.get(temp) != null) {
                Letter letter = letterMap.get(temp);
                letter.setCount(letter.getCount() + 1);
                letterMap.put(temp, letter);
            }
        }

        Set<String> keyset = letterMap.keySet();
        for (String key : keyset) {
            System.out.println( key +"出现的次数：" + letterMap.get(key).getCount());
        }
    }

    public static void test2() {
        String str = "this is a cat and that is a mice and where is the food ?";
        String[] strArray = str.split(" ");
        //hashmap key 不重复，用来计数单词出现重复次数
        Map<String, Letter> letterMap = new HashMap<>();

        for (String temp : strArray) {
            //为所有的key创建容器
            if (!letterMap.containsKey(temp)) {
                letterMap.put(temp, new Letter());
            }

            //之后存放对应的value，容器key重复计数
            if (letterMap.get(temp) != null) {
                Letter letter = letterMap.get(temp);
                letter.setCount(letter.getCount() + 1);
                letterMap.put(temp, letter);
            }
        }

        Set<String> keyset = letterMap.keySet();
        for (String key : keyset) {
            Letter letter = letterMap.get(key);
            System.out.println( key +"出现的次数：" + letter.getCount());
        }
    }
}
